<?php
//20101130 update DataBox UserBox FormBox assist 共通

if (strpos ($_SERVER['PHP_SELF'], 'backuprestore_functions.inc') !== false) {
    die ('This file can not be used on its own.');
}


// +---------------------------------------------------------------------------+
// | configバックアップファイル作成（なければ作成、あれば上書き）
// | 書式 fnc_Backupconfig($pi_name,$name_ary)
// +---------------------------------------------------------------------------+
// | 引数 $pi_name:plugin name 'databox' 'userbox' 'formbox' 'assist'
// | 引数 $name_ary:変数名表
// +---------------------------------------------------------------------------+
// | 戻値 nomal:finish message
// +---------------------------------------------------------------------------+
//
// @@@@@機能追加予定　logをとる
function fnc_Backupconfig(
    $pi_name
    ,$name_ary
)
{
    global $_CONF;

    $box_conf="_".strtoupper($pi_name)."_CONF";
    global $$box_conf;
    $box_conf=$$box_conf;
    $w="";
    for ($i = 1; $i <= count($name_ary); $i++) {
        $nm=current($name_ary);
        if (is_null($box_conf[$nm])){
            $display.=$nm."="."NULL!"."<br>";
        }else{
            if  (is_array($box_conf[$nm])){
                $wk='$_'.strtoupper($pi_name).'_CONF_bak[\''.$nm.'\']= array(';
                $wkary=$box_conf[$nm];

                for ($j = 1; $j <= count($wkary); $j++) {
                    $vl="'".current($wkary)."'";
                    $wk.=$vl.",";
                    next($wkary);
                }
                $wk=rtrim($wk,",");
                $wk.=');'.LB;
            }else{
                $wk='$_'.strtoupper($pi_name).'_CONF_bak[\''.$nm.'\']="'.$box_conf[$nm].'";'.LB;
            }
            $display.=$wk."<br>";
            $w.=$wk;
        }
        next($name_ary);
    }


    //file output open
    $outfile = $_CONF["path_data"].$pi_name."config_bak.php";
    $file = @fopen( $outfile, 'w' );
    if ( $file === false ) {
        $display .= "ERR! ".$outfile ." is not writable!<br />" . LB;
        return $display;
    }
    fwrite($file,'<?php'.LB);
    fwrite($file,$w);
    fwrite($file,'?>'.LB);
    fclose($file);

    $display.="..........".$pi_name." Config Backup finished!"."<br>";
    return $display;
}


// +---------------------------------------------------------------------------+
// | configリストア
// | 書式 fnc_Restoreconfig($pi_name,$config,$name_ary)
// +---------------------------------------------------------------------------+
// | 引数 $pi_name:plugin name 'databox' 'userbox' 'formbox' 'assist'
// | 引数 $config:
// | 引数 $name_ary:変数名表
// +---------------------------------------------------------------------------+
// | 戻値 nomal:finish message
// +---------------------------------------------------------------------------+
// 20111112
// @@@@@機能追加予定　logをとる
function fnc_Restoreconfig(
    $pi_name
    ,$config
    ,$name_ary
)
{
    global $_CONF;

    $box_conf_bak="_".strtoupper($pi_name)."_CONF_bak";

    $display="";

    if (file_exists($_CONF["path_data"].$pi_name."config_bak.php")) {
        require_once( $_CONF["path_data"].$pi_name."config_bak.php" );

        $group=$pi_name;
        $box_conf_bak=$$box_conf_bak;
        for ($i = 1; $i <= count($name_ary); $i++) {
            $nm=current($name_ary);
            if (is_null($box_conf_bak[$nm])){
                $display.=$nm."="."NULL!"."<br>";
            }else{

                $display.=$nm."=".$box_conf_bak[$nm]."<br>";
                $config->set($nm, $box_conf_bak[$nm],$group);
            }
            next($name_ary);
        }

        $display.="..........{$pi_name} Config Restore finished!"."<br>";
        //-----

    }
    return $display;
}

// +---------------------------------------------------------------------------+
// | config削除
// | 書式 fnc_Deleteconfig($pi_name,$config)
// +---------------------------------------------------------------------------+
// | 引数 $pi_name:plugin name 'databox' 'userbox' 'formbox' 'assist'
// | 引数 $config:
// +---------------------------------------------------------------------------+
// | 戻値 nomal:finish message
// +---------------------------------------------------------------------------+
// 20111112
// @@@@@機能追加予定　logをとる
function fnc_Deleteconfig(
    $pi_name
    ,$config
)
{
    global $_TABLES;

    $group=$pi_name;

    DB_delete($_TABLES['conf_values'], 'group_name', $group);
    unset($config->config_array[$group]);

    $box_conf="_".strtoupper($pi_name)."_CONF";
    global $$box_conf;
    $$box_conf=array();

    $display.="..........{$pi_name} Config Delete"."<br>";

    return $display;
}
// +---------------------------------------------------------------------------+
// | config初期化（インストール直後の内容にもどす）
// | 書式 fnc_Initializeconfig($pi_name)
// +---------------------------------------------------------------------------+
// | 引数 $pi_name:plugin name 'databox' 'userbox' 'formbox' 'assist'
// +---------------------------------------------------------------------------+
// | 戻値 nomal:finish message
// +---------------------------------------------------------------------------+
// 20111112
// @@@@@機能追加予定　logをとる
function fnc_Initializeconfig(
    $pi_name
)
{
    global $_CONF;

    $display="";
    //require_once $_CONF['path_system'] . 'classes/config.class.php';
    require_once $_CONF['path'] . 'plugins/'.$pi_name.'/install_defaults.php';

    $function="plugin_initconfig_".$pi_name;
    $rt=$function();

    $display.="..........{$pi_name} Config Initialization finished!!!"."<br>";

    return $display;

}

?>